<?php

/**
 * Class for autoloading all the other classes.
 * This is the only class not controlled by __autoload.
 *
 * @author Ian Clarence <ian.clarence@gmail.com>
 */
class Autoload {

    /**
     * Finding a class "$className" within a directory structure "$dir".
     * Returns the full filepath of the file in which the class is found.
     *
     * @param string $dir
     * @param string $className
     * @return string
     */
    public static function findClass($dir, $className) {
        $handle = opendir($dir);
        $found = FALSE;
        $path = NULL;
        while (($file = readdir($handle)) && ($found == FALSE)) {
            if ($file != "." && $file != ".." && $file[0] != '.') {
                if (is_dir($dir . "/" . $file)) {
                    $path = self::findClass($dir . "/" . $file, $className);
                }
                else if ($file == $className . ".php") {
                    $path = $dir . "/" . $file;
                }
                $found = $path != NULL;
            }
        }
        closedir($handle);
        return $path;
    }
    
}